package api

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"seat-service/initialization"
	"seat-service/response"
	service "seat-service/service/impl"
	"strconv"
	"time"
)

var dataViewService service.DataViewService

type DataViewApi struct {
}

// QueryReserveInformation 查询预约次数
func (d *DataViewApi) QueryReserveInformation(context *gin.Context) {
	reserveInformation, err := dataViewService.QueryReserveInformation()
	if err != nil {
		initialization.SeatLogger.Error("dataViewService.QueryReserveInformation() is failed", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, reserveInformation)
}

// QueryDayReserveCount 按天数查询预约次数
func (d *DataViewApi) QueryDayReserveCount(context *gin.Context) {
	dayStartStr := context.Query("start_day")
	dayEndStr := context.Query("end_day")
	//将字符串转为int64
	dayStart, err := strconv.ParseInt(dayStartStr, 10, 64)
	dayEnd, err := strconv.ParseInt(dayEndStr, 10, 64)
	if err != nil {
		initialization.SeatLogger.Error("strconv.ParseInt(dayStartStr , 10 , 64) is failed", zap.Error(err))
		resp.Fail(context, response.CodeParamError, nil)
		return
	}
	//将时间戳转化为time.Time类型
	startTimeDay := time.Unix(dayStart, 0) //开始时间
	endTimeDay := time.Unix(dayEnd, 0)     //结束时间
	dayCounts, err := dataViewService.QueryReserveDataDay(startTimeDay, endTimeDay)

	if err != nil {
		initialization.SeatLogger.Error("dataViewService.QueryReserveDataDay(startTimeDay , endTimeDay) is error", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, dayCounts)
}

// QueryMonthReserveCount 按月查询预约次数
func (d *DataViewApi) QueryMonthReserveCount(context *gin.Context) {
	monthStartStr := context.Query("start_month")
	monthEndStr := context.Query("end_month")
	//将时间戳转化为年月
	monthStart, err := strconv.ParseInt(monthStartStr, 10, 64)
	monthEnd, err := strconv.ParseInt(monthEndStr, 10, 64)
	if err != nil {
		initialization.SeatLogger.Error("strconv.ParseInt(monthStartStr, 10, 64) is failed", zap.Error(err))
		resp.Fail(context, response.CodeParamError, nil)
		return
	}
	monthStartTime := time.Date(int(monthStart/100), time.Month(monthStart%100), 1, 0, 0, 0, 0, time.UTC)
	monthEndTime := time.Date(int(monthEnd/100), time.Month(monthEnd%100)+1, 1, 0, 0, 0, 0, time.UTC).Add(-time.Second)
	reserveMonthCount, err := dataViewService.QueryReserveDataMonth(monthStartTime, monthEndTime)
	if err != nil {
		initialization.SeatLogger.Error("dataViewService.QueryReserveDataMonth(monthStartTime , monthEndTime) is failed", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, reserveMonthCount)
}

// QueryReserveInformationByCollege 按学院查询预约信息
func (d *DataViewApi) QueryReserveInformationByCollege(context *gin.Context) {
	//接收筛选的类型
	timeType, err := strconv.Atoi(context.Query("time_type"))
	if err != nil || timeType < 0 || timeType >= 3 {
		initialization.SeatLogger.Error("strconv.Atoi(context.Query(\"time_type\")) is failed", zap.Error(err))
		resp.Fail(context, response.CodeParamError, nil)
		return
	}
	reserveInformationByCollege, err := dataViewService.QueryReserveInformationByCollege(uint(timeType))
	if err != nil {
		initialization.SeatLogger.Error("dataViewService.QueryReserveInformationByCollege(uint(timeType)) is failed", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, reserveInformationByCollege)
}

// QueryReserveInformationByClassroom 查询教室预约信息
func (d *DataViewApi) QueryReserveInformationByClassroom(context *gin.Context) {
	reserveClassroomInformation, err := dataViewService.QueryReserveInformationByRoom()
	if err != nil {
		initialization.SeatLogger.Error("dataViewService.QueryReserveInformationByRoom() is failed", zap.Error(err))
		resp.Fail(context, response.CodeServerBusy, nil)
		return
	}
	resp.Success(context, response.CodeSuccess, reserveClassroomInformation)
}

